package com.huaguoguo.miner.monitor.controller;

import com.huaguoguo.miner.monitor.model.Result;
import com.huaguoguo.miner.monitor.model.bo.RegRoot;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 描述：  <br>
 * CreateDate: 2021/8/18 <br>
 *
 * @author huaguoguo
 */
@Slf4j
@RestController
public class GodApi {

    List<RegRoot> miners = new ArrayList<>();
    ReadWriteLock lock = new ReentrantReadWriteLock();

    @PostMapping("/minerStatus")
    public Result<String> registerMiner(RegRoot regRoot) {

        lock.writeLock().lock();
        try {
            miners.add(regRoot);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            lock.writeLock().unlock();
        }
        return Result.success("pull status success");
    }

    @GetMapping("/info")
    public Result<List<RegRoot>> info() {
        lock.readLock().lock();
        try {
            return Result.success(miners);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            lock.readLock().unlock();
        }
        return Result.fail(500,"info error");
    }

}
